package com.program.facesigninsystem.controller;

import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.entity.Rewards;
import com.program.facesigninsystem.service.IRewardService;
import com.program.facesigninsystem.util.GetInfoUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Mapler
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/pro")
public class ProductController {
    private static Logger log = Logger.getLogger(ProductController.class.toString());
    @Autowired
    public IRewardService iRewardService;

    @RequestMapping(value = "/productCreation",method = RequestMethod.POST)
    public ResponseModel productCreation(@RequestBody JSONObject productInfo){
        try{
            String id = GetInfoUtil.getId();
            iRewardService.addNewProducts(
                    id,
                    productInfo.getString("name"),
                    productInfo.getInt("price"),
                    productInfo.getString("caption"),
                    productInfo.getString("imageUrl"),
                    productInfo.getInt("storage"),
                    productInfo.getInt("total")
            );
            return new ResponseModel(200,"添加成功");
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            System.out.println(productInfo);
            return new ResponseModel(124,"添加失败");
        }
    }

    @RequestMapping(value = "/productsList",method = RequestMethod.POST)
    @CurrentLimiter(QPS=5)
    public ResponseModel productsList(){
        try{
            return new ResponseModel(200,"查询成功",iRewardService.selectAllProducts());
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(123,"查询失败");
        }
    }

    @RequestMapping(value = "/productsShowList",method = RequestMethod.POST)
    @CurrentLimiter(QPS=5)
    public ResponseModel productsShowList(){
        try{
            return new ResponseModel(200,"查询成功",iRewardService.selectAllProductsShow());
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(123,"查询失败");
        }
    }

    @RequestMapping(value = "/redeemProduct",method = RequestMethod.POST)
    @CurrentLimiter(QPS=5)
    public ResponseModel redeemProduct(@RequestBody JSONObject info){
        try{
            String id = GetInfoUtil.getId();
            int storage = iRewardService.selectProductById(info.getString("pid")).get("storage");
            int price = iRewardService.selectProductById(info.getString("pid")).get("price");
            if( storage== 0) {
                return new ResponseModel(119,"兑换失败，商品库存为0");
            }
            Rewards rewards = iRewardService.selectReward(info.getString("uid"));
            if(rewards.getReward_points()<price) {
                return new ResponseModel(120,"兑换失败，积分不足");
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentDay = dateFormat.format(System.currentTimeMillis());
            iRewardService.updateRewards(
                    info.getString("uid"),
                    rewards.getLimit_from_record_a(),
                    rewards.getLimit_from_record_b(),
                    rewards.getLimit_from_record_c(),
                    rewards.getLimit_from_activity(),
                    rewards.getLast_gain_day(),
                    rewards.getReward_points()-price,
                    rewards.getDay_reward()
            );
            iRewardService.addNewRedeemRecord(
                    id,
                    info.getString("uid"),
                    info.getString("name"),
                    info.getString("phone"),
                    currentDay,
                    info.getString("province"),
                    info.getString("city"),
                    info.getString("address"),
                    info.getString("pid"),
                    0
            );
            iRewardService.updateProductsStorage(info.getString("pid"), storage - 1);
            return new ResponseModel(200, "兑换成功");
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(121,"兑换失败");
        }
    }

    @RequestMapping(value = "/productSendState",method = RequestMethod.GET)
    public ResponseModel productSendState(@RequestParam("id")String id,@RequestParam("state")int state){
        try{
            iRewardService.updateRedeemRecordState(id,state);
            return new ResponseModel(200,"修改成功");
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(118,"修改失败");
        }
    }

    @RequestMapping(value = "/productAlter",method = RequestMethod.GET)
    public ResponseModel productAlter(@RequestParam("show")int show,@RequestParam("id")String id){
        try{
            iRewardService.updateProductShow(show,id);
            return new ResponseModel(200,"修改成功");
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(118,"修改失败");
        }
    }

    @RequestMapping(value = "/redeemRecordListAll",method = RequestMethod.POST)
    public ResponseModel redeemRecordListAll(){
        try{
            return new ResponseModel(200,"查询成功",iRewardService.selectAllRedeemRecord());
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(117,"查询失败");
        }
    }

    @RequestMapping(value = "/redeemRecordNotSend",method = RequestMethod.POST)
    public ResponseModel redeemRecordNotSend(){
        try{
            return new ResponseModel(200,"查询成功",iRewardService.selectRecordNotSend());
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(117,"查询失败");
        }
    }

    @RequestMapping(value = "/redeemRecordAlreadySend",method = RequestMethod.POST)
    public ResponseModel redeemRecordAlreadySend(){
        try{
            return new ResponseModel(200,"查询成功",iRewardService.selectRecordAlreadySend());
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(117,"查询失败");
        }
    }

    @RequestMapping(value = "/redeemRecordListMy",method = RequestMethod.GET)
    public ResponseModel redeemRecordListMy(@RequestParam("uid")String uid){
        try{
            return new ResponseModel(200,"查询成功",iRewardService.selectMyRedeemRecord(uid));
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(117,"查询失败");
        }
    }
}
